<?php
class EmployeesController extends AppController {

	var $name = 'Employees';
	function admin_index() {
		$this->Employee->recursive = 0;
		$this->set('employees', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('employee', $this->Employee->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Employee->create();
			//$this->data['Employee']['pass_word'] = $this->Auth->password($this->data['Employee']['pass_word']);
			if ($this->Employee->save($this->data)) {
				$this->Session->setFlash(__('The employee has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
		}
		$positions = $this->Employee->Position->find('list');
		$this->set(compact('positions'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Employee->save($this->data)) {
				$this->Session->setFlash(__('The employee has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employee->read(null, $id);
		}
		$positions = $this->Employee->Position->find('list');
		$this->set(compact('positions'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for employee', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Employee->delete($id)) {
			$this->Session->setFlash(__('Employee deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Employee was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//---------------------------------------------
	function admin_login(){
		//echo session_id();
		//$this->autoRender = false;
		//print_r($this->data);
	}
	
	function admin_mlogin(){
		$this->autoRender = false;
		/*
		$_POST['logininfor'] = '<?xml version="1.0" encoding="UTF-8"?>'.
								'<login_infor>'.
									'<user_name>admin</user_name>'.
									'<pass_word>12345</pass_word>'.
								'</login_infor>';
		*/
		// nếu thông tin truyền lên 
		if(isset($_POST['logininfor'])){
			// tách data từ xml
			$dataXML = $_POST['logininfor'];
			$xml =simplexml_load_string($dataXML);
			foreach ($xml as $key=>$value){
				$data['Employee'][$key] = (string)$value;
			}
			if(isset($data['Employee']['pass_word'])){
				$data['Employee']['pass_word'] =$this->Auth->password($data['Employee']['pass_word']);
				// nếu login thành công
				if($this->Auth->login($data)){
					$employee = $this->Session->read('Auth.Employee');
					$result = '<?xml version="1.0" encoding="UTF-8"?>'.
						'<response>'.
							'<status>1</status>'.
							'<employee_id>'.$employee['id'].'</employee_id>'.
							'<employee_name>'.$employee['employee_name'].'</employee_name>'.
							'<session_id>'.session_id().'</session_id>'.
						'</response>';
					echo $result;				
					return;
				}
			}
		}
		$result = '<?xml version="1.0" encoding="UTF-8"?>'.
					'<response>'.
						'<status>0</status>'.
						'<message>Truyền sai dữ liệu</message>'.
					'</response>';
		echo $result;
	}
	
	function admin_logout(){
		$this->redirect($this->Auth->logout());
	}
}
?>